<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      :small="totalCount > 50 ? true : false"
      :background="background"
      :current-page="Page"
      :page-size="limit"
      :layout="layoutNum"
      :page-sizes="pageSizes"
      :total="totalCount"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :hide-on-single-page="hideonsinglepage"
    />
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'

export default defineComponent({
  props: {
    totalCount: {
      required: true,
      type: Number
    },
    currentPage: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    },
    hideonsinglepage: {
      type: Boolean,
      default: false
    },
    pageSizes: {
      type: Array,
      default: () => ([10, 20, 50, 100])
    },
    layout: {
      type: String,
      default: 'sizes,total,prev, pager, next'
    },
    background: {
      type: Boolean,
      default: true
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  setup(props, { emit }) {
    const Page = computed({
      get() {
        return props.currentPage
      },
      set(val) {
        emit('update:currentPage', val)
      }
    })
    const layoutNum = computed({
      get() {
        return props.layout ? props.layout : 'sizes,total,prev, pager, next'
      },
      set(val) {
        emit('update:currentPage', val)
      }
    })
    const limit = computed({
      get() {
        return props.pageSize
      },
      set(val) {
        emit('update:pageSize', val)
      }
    })
    const handleSizeChange = (val: any) => {
      const data = { pageSize: val }
      emit('pageChange', data)
    }
    const handleCurrentChange = (val: any) => {
      const data = { currentPage: val }
      emit('pageChange', data)
    }
    return {
      Page,
      limit,
      layoutNum,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style lang="less" scoped>
.pagination-container {
  background: #fff;
  padding: 32px 16px;
  text-align: right;
}
</style>
